package com.esl.action.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.stereotype.Controller;

import com.esl.action.CommonAction;
import com.esl.dto.admin.User_Dto;
import com.esl.model.admin.User;
import com.esl.service.admin.UserManager;
import com.esl.util.IPRecordUtils;
//@Controller
public class LoginAction extends CommonAction implements ServletRequestAware {
	private Map<String, Object> session = getSession();
	private User_Dto user_Dto = new User_Dto();
	private UserManager userManager;
	private HttpServletRequest httpServletRequest;
	private IPRecordUtils ipRecordUtils;

	@Override
	public String execute() {
		if (getUid() != -1)
			return "toIndex";
		else
			return getJump_url();
	}

	public String login() {
		if (user_Dto.getAuthcode().equals(session.get("picValidate"))) {
			User_Dto userDto = userManager.check(user_Dto.getStudentNO(),
					user_Dto.getPassword());
			if (userDto.getId() != -1) {
				session.remove("picValidate");
				session.put("uid", userDto.getId());
				session.put("name", userDto.getName());
				userManager.RecodeLogByLogin(userDto.getName(),
						ipRecordUtils.getIpAddr(httpServletRequest));
				return "toIndex";
			}
		}
		return "toLogin";
	}

	public String logout() {
		session.clear();
		return "toLogin";
	}

	/*--------------------------------*/
	public User_Dto getUser_Dto() {
		return user_Dto;
	}

	public void setUser_Dto(User_Dto user_Dto) {
		this.user_Dto = user_Dto;
	}

	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.httpServletRequest = httpServletRequest;
	}

	/*--------------------------------*/
	@Resource
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	@Resource
	public void setUserManger(UserManager userManager) {
		this.userManager = userManager;
	}

	@Resource
	public void setIpRecordUtils(IPRecordUtils ipRecordUtils) {
		this.ipRecordUtils = ipRecordUtils;
	}

}
